Warning: package 'sf' was built under R version 4.3.3
Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
terra 1.7.55
Attaching package: 'dplyr'
The following objects are masked from 'package:terra':
intersect, union
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
Loading required package: viridisLite
Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
remotes::install_github('r-tmap/tmap')
Attaching package: 'tidyr'
The following object is masked from 'package:terra':
extract
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
dat <- vroom(...)
problems(dat)
Rows: 1751442 Columns: 104
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (66): compositepropertylinkagekey, originalapn, onlineformattedparcelid,...
dbl (31): clip, fips, fipscode, apnparcelnumberunformatted, apnsequencenumbe...
lgl (7): previousclip, taxaccountnumber, mobilehomeindicator, effectiveyear...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
dat <- vroom(...)
problems(dat)
Warning: Number of levels of the variable "Name" is 80, which is larger than
max.categories (which is 30), so levels are combined. Set
tmap_options(max.categories = 80) in the layer function to show all levels.
To adjoin we will focus on matching with buffer of the reserve location and use the tax plot match to adjoin to the property data. Add a buffer distance of 10 meters and then match with the Hawaii tmz file.
Code
reserves <-st_transform(reserves, 32605)reserves_buffered <-st_buffer(reserves, dist =10)reserves_buffered <-st_transform(reserves_buffered, 4326)intersections <-st_intersects(HI_2023_shapefile, reserves_buffered)HI_intersections <- HI_2023_shapefile[lengths(intersections) >0, ]ggplot() +geom_sf(data = HI_intersections, fill ="brown", color ="grey") +geom_sf(data = reserves_buffered, fill ="forestgreen", color ="black") +geom_sf(data = coastline, color ="black") +coord_sf(xlim =c(-158.5, -157.5), # longitudesylim =c(21, 21.9), # latitudes in correct orderexpand =FALSE ) +theme_minimal() +labs(title ="Buffered Reserves (Green) and Adjoining Properties (Brown)")
The adjoining part will join shapefile to properties with Name of reserve, who manages it and the type of reserve it is.
Code
# Create a vector of indices for the first overlapping shp2 feature per shp1 rowHI_2023_shapefile$reserve_names <-sapply(intersections, function(idx) {if (length(idx) ==0) {return(NA_character_) } else {paste(reserves_buffered$name[idx], collapse =", ") }})HI_2023_shapefile$managedby <-sapply(intersections, function(idx) {if (length(idx) ==0) {return(NA_character_) } else {paste(reserves_buffered$managedby[idx], collapse =", ") }})HI_2023_shapefile$type_defin <-sapply(intersections, function(idx) {if (length(idx) ==0) {return(NA_character_) } else {paste(reserves_buffered$type_defin[idx], collapse =", ") }})
Source Code
---format: html: code-fold: true # Enables dropdown for code code-tools: true # (Optional) Adds buttons like "Show Code" code-summary: "Show code" # (Optional) Custom label for dropdown toc: true toc-location: left page-layout: fulleditor: visual---# Amenities## Golf Courses```{r}source("_common.R")golf=read_sf("/Users/ashleylowe/Trails/golf_courses.shp/golf_courses.shp")golf=st_transform(golf, 4326 )golf=golf[c(1,7,8,12)]tmap_mode("view")center_lon <--157.45center_lat <-20.75zoom_level <-7tm_shape(golf) +tm_dots(col ="Name",palette ="magma",size =0.1,id ="Name",popup.vars =c("Name"="Name"),legend.show =FALSE# hides the legend ) +tm_basemap() +tm_view(set.view =c(center_lon, center_lat, zoom_level))```Distance to each Property calculation```{r eval=FALSE}# 1. find nearest feature indexnearest_idx <-st_nearest_feature(PB_HawaiiInfo, golf)# 2. get nearest trails (same row order as PB_HawaiiInfo)nearest_trails <- golf[nearest_idx, ]# 3. calculate distance (element-wise)nearest_dist_m <-st_distance(PB_HawaiiInfo, nearest_trails, by_element =TRUE)# 4. merge trail attributes + distance into PB_HawaiiInfoPB_HawaiiInfo <- PB_HawaiiInfo %>%mutate(nearest_golf =as.numeric(nearest_dist_m) ) %>%bind_cols(st_drop_geometry(nearest_trails)) # adds trail attributes```## Nature ReservesCentralized location of managed lands protecting natural and cultural resources of the Main Hawaiian Islands and Kure Atoll.We will use this one to create a buffer and identify the properties that adjoin a reserve.```{r cache=TRUE, echo=TRUE, warning=FALSE, message=FALSE}reserves <-read_sf("/Users/ashleylowe/Trails/Reserves/Reserves.shp")reserves$managedby <-gsub("[()]", "", reserves$managedby)tmap_mode("view")center_lon <--157.45center_lat <-20.75zoom_level <-7tm_shape(reserves) +tm_polygons("managedby",palette ="magma",id ="name",popup.vars =c("Managed By"="managedby"),legend.show =FALSE# hides the legend ) +tm_basemap() +tm_view(set.view =c(center_lon, center_lat, zoom_level))```To adjoin we will focus on matching with buffer of the reserve location and use the tax plot match to adjoin to the property data. Add a buffer distance of 10 meters and then match with the Hawaii tmz file.```{r cache=TRUE, echo=TRUE, warning=FALSE, message=FALSE}reserves <-st_transform(reserves, 32605)reserves_buffered <-st_buffer(reserves, dist =10)reserves_buffered <-st_transform(reserves_buffered, 4326)intersections <-st_intersects(HI_2023_shapefile, reserves_buffered)HI_intersections <- HI_2023_shapefile[lengths(intersections) >0, ]ggplot() +geom_sf(data = HI_intersections, fill ="brown", color ="grey") +geom_sf(data = reserves_buffered, fill ="forestgreen", color ="black") +geom_sf(data = coastline, color ="black") +coord_sf(xlim =c(-158.5, -157.5), # longitudesylim =c(21, 21.9), # latitudes in correct orderexpand =FALSE ) +theme_minimal() +labs(title ="Buffered Reserves (Green) and Adjoining Properties (Brown)")```The adjoining part will join shapefile to properties with Name of reserve, who manages it and the type of reserve it is.```{r eval=FALSE}# Create a vector of indices for the first overlapping shp2 feature per shp1 rowHI_2023_shapefile$reserve_names <-sapply(intersections, function(idx) {if (length(idx) ==0) {return(NA_character_) } else {paste(reserves_buffered$name[idx], collapse =", ") }})HI_2023_shapefile$managedby <-sapply(intersections, function(idx) {if (length(idx) ==0) {return(NA_character_) } else {paste(reserves_buffered$managedby[idx], collapse =", ") }})HI_2023_shapefile$type_defin <-sapply(intersections, function(idx) {if (length(idx) ==0) {return(NA_character_) } else {paste(reserves_buffered$type_defin[idx], collapse =", ") }})```